/**
 * Created by Administrator on 2015/2/25.
 */
function setHeaderUserName(){$("#headerUserName").html($("#homeUserName").val());}
var homeApp = angular.module("app.homeInfo", []);
//用户操作头部
homeApp.controller("header",['$scope','$http','$timeout','ngDialog','$location',function($scope,$http,$timeout,ngDialog,$location){
    $scope.loginOut = function(){
        $http.get('management/userHandle/loginOut').success(function(data){
            location.href = 'login';
        });
    };
    //用户修改地区
    $scope.userSetZone = function(id){
        $http.get('management/user/userSetZone?id='+id).success(function(data){
            var newUrl="";var url = $location.absUrl().toString();
            if(url.indexOf("?")<0)newUrl = url.substring(0,url.indexOf("#"));
            else newUrl = url.substring(0,url.indexOf("?"));
            location.href=newUrl+"?admin="+Math.random()+url.substring(url.indexOf("#"));
        });
    };
    //修改密码
    $scope.openUpdatePwdDialog = function(){
        ngDialog.open({template: 'management/userHandle/updatePwd',
        className:'ngdialog-theme-flat', width:800, height:470});
    };
    $scope.updatePwdForm = {};$scope.input = {};
    $scope.input.nextPwd = "";$scope.updatePwdForm.newPwd = "";
    $scope.nextPwdPass = false;
    $scope.nextPwdVail = function(){
        if($scope.input.nextPwd != $scope.updatePwdForm.newPwd){
            $("#nextPwdStyle").attr('style',"border:1px solid red");$scope.nextPwdPass = false;
        }else{
            $("#nextPwdStyle").attr('style',"");$scope.nextPwdPass = true;
        }
    };
    $scope.updatePwd = function(){
        if(!$scope.nextPwdPass){return;}
        $scope.isPwdHide = false;
        $http({
            method  : 'post', url     : 'management/userHandle/updatePwdData',
            data    : $.param($scope.updatePwdForm), headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function(data){
            if(data.length == 0){
            $scope.showInfo = "改 密 失 败 你 的 密 码 有 误"}else{
            $scope.showInfo = "改 密 成 功 下 次 登 录 记 得 用 新 密 码";
            $timeout(function(){ngDialog.close('management/userHandle/updatePwd');},1000);}
        }).error(function(data){
            $scope.showInfo = "改 密 失 败 你 的 密 码 有 误";});
        $timeout(function(){$scope.isPwdHide = true;},5000);
    }
}]);
//用户操作主页
function setJobState(job){if(job == 1)return '试用'; else if(job == 2)return '转正'; else return '离职';}
homeApp.controller("userHome",['$scope','$http','$timeout','ngDialog',function($scope,$http,$timeout,ngDialog){
    $http.get('management/userHandle/getUserInfo').success(function(data){
        $scope.userInfo = data;$scope.userInfo.jobstate = setJobState($scope.userInfo.jobstate);
        $("#userName").html(data.realname);
    });$scope.info = false;$scope.input = true;$scope.setShow = function(){$scope.info = true;$scope.input = false;}
    $scope.seHide = function(){$scope.info = false;$scope.input = true;}
    //主页修改信息
    $scope.setValue = function(){
        $scope.submitData={};$scope.submitData.id = $scope.userInfo.id;
        $scope.submitData.realname = $scope.userInfo.realname;
        $scope.submitData.phone1 = $scope.userInfo.phone1;
        $scope.submitData.email = $scope.userInfo.email;
        $scope.submitData.address = $scope.userInfo.address;
        $http({
            method  : 'post',
            url     : 'management/user/userUpdate',
            data    : $.param($scope.submitData),
            headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function(data){
            ngDialog.open({
                template: 'management/userHandle/remind',showClose:false,
                remind: '操作成功', width:300, height:110});setHeaderUserName();
            $("#userName").html($("#homeUserName").val());
            $scope.info = false;$scope.input = true;
        }).error(function(data){
            ngDialog.open({template: 'management/userHandle/remind',showClose:false,
                remind: '服务器繁忙稍后再试', width:350, height:110});
        });
        $timeout(function(){ngDialog.close('management/userHandle/remind')},1500);
    }
}]);